package vip.xiaonuo.util;

import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.filter.Filter;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Iterator;

/**
 * ShpDemo
 *
 * @author JackLee
 * 2022/7/25 10:51
 */
public class ShpDemo {

    public static void main(String[] args) throws IOException {

        File shapeFile = new File("C:\\Users\\24625\\Desktop\\绿地面数据.shp");
//        ShapefileDataStore store = new ShapefileDataStore(shapeFile.toURI().toURL());
//        Charset charset = Charset.forName("UTF-8");
//        store.setCharset(charset);
//        SimpleFeatureSource sfSource = store.getFeatureSource();
//        SimpleFeatureIterator featureIterator = sfSource.getFeatures().features();
//
//        int i = 0;
//        int j = 0;
//        while (featureIterator.hasNext()) {
//            i++;
//            SimpleFeature feature = featureIterator.next();
//            Iterator<? extends Property> iterator = feature.getValue().iterator();
//
//            while (iterator.hasNext()) {
//                j++;
//                Property property = iterator.next();
//                System.out.println("【" + i + "-" + j + "】" + property.getName() + ">>>" + property.getValue().toString());
//            }
//            j = 0;
//        }

        String path1 = "D:\\WPS同步文件夹\\项目\\武大可视化平台\\时空地理数据\\2022年时空数据\\绿地面积\\我国绿地面积\\绿地面数据.shp";
//        String path1 = "C:\\Users\\24625\\Desktop\\绿地面数据.shp";

        //读取shp
        SimpleFeatureCollection  colls1 = readShp(path1);
        //拿到所有features
        SimpleFeatureIterator iters = colls1.features();
        //遍历打印
        while(iters.hasNext()){
            SimpleFeature sf = iters.next();
            Iterator<? extends Property> iterator = sf.getValue().iterator();

            while (iterator.hasNext()) {
                Property property = iterator.next();
                System.out.println(property.getName() + ">>>" + property.getValue().toString());
            }

//            System.out.println(sf.getID() + " , " + sf.getAttributes());

        }
    }
    public static SimpleFeatureCollection readShp(String path){
        return readShp(path, null);

    }

    public static SimpleFeatureCollection  readShp(String path , Filter filter){

        SimpleFeatureSource  featureSource = readStoreByShp(path);

        if(featureSource == null) return null;

        try {
            return filter != null ? featureSource.getFeatures(filter) : featureSource.getFeatures() ;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null ;
    }

    public static  SimpleFeatureSource readStoreByShp(String path ){

        File file = new File(path);

        FileDataStore store;
        SimpleFeatureSource featureSource = null;
        try {
            store = FileDataStoreFinder.getDataStore(file);
            ((ShapefileDataStore) store).setCharset(Charset.forName("UTF-8"));
            featureSource = store.getFeatureSource();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return featureSource ;
    }



}
